package org.dfzt.modules.online.config.service.a;

import org.apache.commons.lang.StringUtils;
import org.dfzt.modules.online.config.service.DbTableHandleI;


public class MysqlHandler implements DbTableHandleI {
    @Override
    public String getAddColumnSql(org.dfzt.modules.online.config.util.a columnMeta) {
        return " ADD COLUMN " + a(columnMeta) + ";";
    }


    @Override
    public String getReNameFieldName(org.dfzt.modules.online.config.util.a columnMeta) {
        return "CHANGE COLUMN " + columnMeta.getOldColumnName() + " " + b(columnMeta) + " ;";
    }


    @Override
    public String getUpdateColumnSql(org.dfzt.modules.online.config.util.a cgformcolumnMeta, org.dfzt.modules.online.config.util.a datacolumnMeta) {
        return " MODIFY COLUMN " + b(cgformcolumnMeta, datacolumnMeta) + ";";
    }


    @Override
    public String getMatchClassTypeByDataType(String dataType, int digits) {
        String str = "";
        if (dataType.equalsIgnoreCase("varchar")) {
            str = "string";
        } else if (dataType.equalsIgnoreCase("double")) {
            str = "double";
        } else if (dataType.equalsIgnoreCase("int")) {
            str = "int";
        } else if (dataType.equalsIgnoreCase("Date")) {
            str = "date";
        } else if (dataType.equalsIgnoreCase("Datetime")) {
            str = "date";
        } else if (dataType.equalsIgnoreCase("decimal")) {
            str = "bigdecimal";
        } else if (dataType.equalsIgnoreCase("text")) {
            str = "text";
        } else if (dataType.equalsIgnoreCase("blob")) {
            str = "blob";
        }
        return str;
    }


    @Override
    public String dropTableSQL(String tableName) {
        return " DROP TABLE IF EXISTS " + tableName + " ;";
    }


    @Override
    public String getDropColumnSql(String fieldName) {
        return " DROP COLUMN " + fieldName + ";";
    }


    private String a(org.dfzt.modules.online.config.util.a parama1, org.dfzt.modules.online.config.util.a parama2) {
        String str1 = "";
        if (parama1.getColunmType().equalsIgnoreCase("string")) {
            str1 = parama1.getColumnName() + " varchar(" + parama1.getColumnSize() + ") " + ("Y".equals(parama1.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (parama1.getColunmType().equalsIgnoreCase("date")) {
            str1 = parama1.getColumnName() + " datetime " + ("Y".equals(parama1.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (parama1.getColunmType().equalsIgnoreCase("int")) {
            str1 = parama1.getColumnName() + " int(" + parama1.getColumnSize() + ") " + ("Y".equals(parama1.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (parama1.getColunmType().equalsIgnoreCase("double")) {
            str1 = parama1.getColumnName() + " double(" + parama1.getColumnSize() + "," + parama1.getDecimalDigits() + ") " + ("Y".equals(parama1.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (parama1.getColunmType().equalsIgnoreCase("bigdecimal")) {
            str1 = parama1.getColumnName() + " decimal(" + parama1.getColumnSize() + "," + parama1.getDecimalDigits() + ") " + ("Y".equals(parama1.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (parama1.getColunmType().equalsIgnoreCase("text")) {
            str1 = parama1.getColumnName() + " text " + ("Y".equals(parama1.getIsNullable()) ? "NULL" : "NOT NULL");
        } else if (parama1.getColunmType().equalsIgnoreCase("blob")) {
            str1 = parama1.getColumnName() + " blob " + ("Y".equals(parama1.getIsNullable()) ? "NULL" : "NOT NULL");
        }
        str1 = str1 + (StringUtils.isNotEmpty(parama1.getComment()) ? (" COMMENT '" + parama1.getComment() + "'") : " ");
        str1 = str1 + (StringUtils.isNotEmpty(parama1.getFieldDefault()) ? (" DEFAULT " + parama1.getFieldDefault()) : " ");
        String str2 = parama1.getPkType();
        if ("id".equalsIgnoreCase(parama1.getColumnName()) && str2 != null && ("SEQUENCE"
                .equalsIgnoreCase(str2) || "NATIVE".equalsIgnoreCase(str2))) {
            str1 = str1 + " AUTO_INCREMENT ";
        }
        return str1;
    }


    private String b(org.dfzt.modules.online.config.util.a parama1, org.dfzt.modules.online.config.util.a parama2) {
        return a(parama1, parama2);
    }


    private String a(org.dfzt.modules.online.config.util.a parama) {
        return a(parama, null);
    }


    private String b(org.dfzt.modules.online.config.util.a parama) {
        return a(parama, null);
    }


    @Override
    public String getCommentSql(org.dfzt.modules.online.config.util.a columnMeta) {
        return "";
    }


    @Override
    public String getSpecialHandle(org.dfzt.modules.online.config.util.a cgformcolumnMeta, org.dfzt.modules.online.config.util.a datacolumnMeta) {
        return null;
    }


    @Override
    public String dropIndexs(String indexName, String tableName) {
        return "DROP INDEX " + indexName + " ON " + tableName;
    }


    @Override
    public String countIndex(String indexName, String tableName) {
        return "select COUNT(*) from information_schema.statistics where table_name = '" + tableName + "'  AND index_name = '" + indexName + "'";
    }
}
